package ar.com.ford.it.fullEconomics.view.form;

import java.util.Collection;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.MessageResources;
import org.apache.struts.validator.ValidatorForm;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesf01Fulleconom;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesr02Requestitm;
import ar.com.ford.it.fullEconomics.util.FullEconomicsDateUtil;

public class RequestPopupForm extends ValidatorForm {
    private String section;
    private String supplierClass;
    private String supplierCode;
    private String commodityCode;
    private String partClass;
    private String prefix;
    private String base;
    private String suffix;
    private String dateFilter;
    private String dateFrom;
    private String dateTo;
    private String status;
    private String fulleCode;
    private String userField;
    private String userCode;
    private Teesf01Fulleconom fullEconomic;
    private Teesr02Requestitm requestItemSelected;
    private Collection<Teesr02Requestitm> requestItemColl;

    public void resetForm() {
        this.section = "init";;
        this.commodityCode = "-1";
        this.partClass = "-1";
        this.base = null;
        this.suffix = null;
        this.dateFrom = null;
        this.dateTo = null;
        this.status = null;
        this.requestItemSelected = null;
        this.requestItemColl = null;
    }

   
    public ActionErrors validate(ActionMapping mapping,
                                 HttpServletRequest request) {

        ActionErrors ae = new ActionErrors();
        MessageResources resources = (MessageResources) request
                .getAttribute(org.apache.struts.Globals.MESSAGES_KEY);
        if ("find".equalsIgnoreCase(this.getSection())) {
            if (!StringUtils.isBlank(this.getDateFrom())) {
                if (StringUtils.isBlank(this.getDateTo())) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "request.error.invalidDateRange"));
                }
                try {
                    if (FullEconomicsDateUtil.format(this.getDateFrom()) == null) {
                        ae.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("errors.date", this
                                        .getDateFrom()));
                    }
                } catch (Exception e) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "errors.date", this.getDateFrom()));
                }
            }
            if (!StringUtils.isBlank(this.getDateTo())) {
                if (StringUtils.isBlank(this.getDateFrom())) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "request.error.invalidDateRange"));
                }
                try {
                    if (FullEconomicsDateUtil.format(this.getDateTo()) == null) {
                        ae.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("errors.date", this
                                        .getDateTo()));
                    }
                } catch (Exception e) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "errors.date", this.getDateTo()));
                }
            }
        }
        return ae;
    }

    public void setRequestMapped(String key, String value) {
        List requests = (List) this.getRequestItemColl();
        if (requests != null)
            this.setRequestItemSelected((Teesr02Requestitm) requests.get(Integer
                    .parseInt(key) - 1));
    }

    public String getBase() {
        return base;
    }

    public void setBase(String base) {
        this.base = base.toUpperCase();
    }

    public String getCommodityCode() {
        return commodityCode;
    }

    public void setCommodityCode(String commodityCode) {
        this.commodityCode = commodityCode;
    }

    public String getDateFrom() {
        return dateFrom;
    }

    public void setDateFrom(String dateFrom) {
        this.dateFrom = dateFrom;
    }

    public String getDateTo() {
        return dateTo;
    }

    public void setDateTo(String dateTo) {
        this.dateTo = dateTo;
    }

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix.toUpperCase();
    }

    public String getSection() {
        return section;
    }

    public void setSection(String section) {
        this.section = section;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status.toUpperCase();
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix.toUpperCase();
    }

    public String getPartClass() {
        return partClass;
    }

    public void setPartClass(String partClass) {
        this.partClass = partClass.toUpperCase();
    }

    public Teesr02Requestitm getRequestSelected(String selectedIndex) {
        List requests = (List) this.getRequestItemColl();

        this.setRequestItemSelected((Teesr02Requestitm) requests.get(Integer
                .parseInt(selectedIndex) - 1));
        return requestItemSelected;
    }

    public String getUserCode() {
        return userCode;
    }

    public void setUserCode(String userCode) {
        this.userCode = userCode;
    }

    public String getUserField() {
        return userField;
    }

    public void setUserField(String userField) {
        this.userField = userField;
    }

    public String getDateFilter() {
        return dateFilter;
    }

    public void setDateFilter(String dateFilter) {
        this.dateFilter = dateFilter;
    }


    public Collection<Teesr02Requestitm> getRequestItemColl() {
        return requestItemColl;
    }


    public void setRequestItemColl(Collection<Teesr02Requestitm> requestItemColl) {
        this.requestItemColl = requestItemColl;
    }


    public Teesr02Requestitm getRequestItemSelected() {
        return requestItemSelected;
    }


    public void setRequestItemSelected(Teesr02Requestitm requestItemSelected) {
        this.requestItemSelected = requestItemSelected;
    }


    public Teesf01Fulleconom getFullEconomic() {
        return fullEconomic;
    }


    public void setFullEconomic(Teesf01Fulleconom fullEconomic) {
        this.fullEconomic = fullEconomic;
    }


    public String getFulleCode() {
        return fulleCode;
    }


    public void setFulleCode(String fulleCode) {
        this.fulleCode = fulleCode;
    }


    public String getSupplierClass() {
        return supplierClass;
    }


    public void setSupplierClass(String supplierClass) {
        this.supplierClass = supplierClass;
    }


    public String getSupplierCode() {
        return supplierCode;
    }


    public void setSupplierCode(String supplierCode) {
        this.supplierCode = supplierCode;
    }

}
